package home

import (
	"context"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/apps/sorting/auth"
	"insighttracking/common/enum"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/sorting/internal/svc"
	"insighttracking/apps/sorting/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetHomeApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetHomeApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetHomeApiLogic {
	hostname, _ := os.Hostname()
	return &GetHomeApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetHomeApiLogic) GetHomeApi(req *types.GetHomeReq) (resp *types.GetHomeResp, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return nil, err
	}
	// 1.查询当前企业下全部的订单
	ordersEntities, err := service.OrdersService.GetSendGoodsDateListApi(l.ctx, accountInfo.EnterpriseId, req.StartDate, req.EndDate, req.WarehouseID)
	if err != nil || len(ordersEntities) == 0 {
		return &types.GetHomeResp{
			TotalCount:          0, // 订单商品总数量
			SortingCount:        0, // 已经分拣的数量
			OrdersCount:         0, // 订单数量
			CompleteOrdersCount: 0, // 完成订单数量
		}, err
	}
	// 查询订单数量
	orderIdList := k.Map(ordersEntities, func(item *model.OrdersEntity, index int) int64 {
		return item.ID
	})
	ordersDetailEntities, err := service.OrdersDetailService.GetByOrderIdListApi(l.ctx, orderIdList)
	orderDetailIdList := k.Map(ordersDetailEntities, func(item *model.OrdersDetailEntity, index int) int64 {
		return item.ID
	})
	logx.WithContext(l.ctx).Infof("订单详情数据:%v", ordersDetailEntities)
	if err != nil || len(ordersDetailEntities) == 0 {
		return &types.GetHomeResp{
			TotalCount:          0, // 订单商品总数量
			SortingCount:        0, // 已经分拣的数量
			OrdersCount:         0, // 订单数量
			CompleteOrdersCount: 0, // 完成订单数量
		}, err
	}
	var ordersCount = len(ordersEntities) // 订单数量

	var totalCount = int64(len(ordersDetailEntities)) // 订单商品总数量
	var sortingCount int64 = 0                        // 分拣数量

	// 查询已经分拣的重量
	orderDetailIdList = k.Map(ordersDetailEntities, func(item *model.OrdersDetailEntity, index int) int64 {
		return item.ID
	})
	entities, err := service.StoreSortingDetailService.GetStoreSortingDetailByOrdersDetailIdListApi(l.ctx, orderDetailIdList)
	if err == nil && len(entities) > 0 {
		sortingCount = int64(len(entities))
	}
	var completeOrdersCount = 0 // 完成订单数量
	for _, item := range ordersEntities {
		if item.Status >= enum.OrdersSortingFinishEnum {
			completeOrdersCount += 1
		}
	}
	for _, item := range ordersEntities {
		detailEntities, err := service.OrdersDetailService.GetByOrderIdListApi(l.ctx, []int64{item.ID})
		if err == nil && len(detailEntities) > 0 {
			// 查询分拣的数据
			orderDetailIdList1 := k.Map(detailEntities, func(item *model.OrdersDetailEntity, index int) int64 {
				return item.ID
			})
			sortingDetailEntities, err := service.StoreSortingDetailService.GetStoreSortingDetailByOrdersDetailIdListApi(l.ctx, orderDetailIdList1)
			if err == nil && len(sortingDetailEntities) == len(detailEntities) {
				completeOrdersCount += 1
			}
		}
	}
	return &types.GetHomeResp{
		TotalCount:          totalCount,                 // 订单商品总数量
		SortingCount:        sortingCount,               // 已经分拣的重量
		OrdersCount:         int64(ordersCount),         // 订单数量
		CompleteOrdersCount: int64(completeOrdersCount), // 完成订单数量
	}, nil
}
